package com.zhaojonhy.zhaopangwb.Fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.exception.WeiboException;
import com.sina.weibo.sdk.net.RequestListener;
import com.sina.weibo.sdk.openapi.StatusesAPI;
import com.sina.weibo.sdk.openapi.UsersAPI;
import com.zhaojonhy.zhaopangwb.R;
import com.zhaojonhy.zhaopangwb.bean.User;
import com.zhaojonhy.zhaopangwb.ui.CommentToMeActivity;
import com.zhaojonhy.zhaopangwb.ui.MainActivity;
import com.zhaojonhy.zhaopangwb.ui.SelfStatuseActivity;
import com.zhaojonhy.zhaopangwb.utils.AccessTokenKeeper;
import com.zhaojonhy.zhaopangwb.utils.BaseApplication;
import com.zhaojonhy.zhaopangwb.utils.Constants;
import com.zhaojonhy.zhaopangwb.utils.ToastUtils;
import com.zhaojonhy.zhaopangwb.utils.TitleBuilder;
import com.zhaojonhy.zhaopangwb.widget.Pull2RefreshListView;

/**
 * Created by Administrator on 2016/9/5.
 */
public class MessageFragment extends BaseFragment implements View.OnClickListener {

    private static final String TAG = MessageFragment.class.getName();

    private View view ;
    private LinearLayout ll_at ;
    private LinearLayout ll_comments ;
    private LinearLayout ll_like ;

    /** 当前 Token 信息 */
    private Oauth2AccessToken mAccessToken;
    private UsersAPI mUsersAPI;
    private User user ;
    private Pull2RefreshListView plv_msg ;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = View.inflate(activity, R.layout.frag_msg,null) ;

        mAccessToken = AccessTokenKeeper.readAccessToken(activity) ;
        mUsersAPI = new UsersAPI(activity, Constants.APP_KEY, mAccessToken) ;

        initTitle() ;
        initMsg();

        return view;
    }

    private void initTitle() {
        new TitleBuilder(view).setTitleText("消息") ;
    }

    private void initMsg() {
        ll_at = (LinearLayout)view.findViewById(R.id.ll_at) ;
        ll_comments = (LinearLayout)view.findViewById(R.id.ll_comments) ;
        ll_like = (LinearLayout)view.findViewById(R.id.ll_like) ;

        ll_at.setOnClickListener(this);
        ll_comments.setOnClickListener(this);
        ll_like.setOnClickListener(this);
    }

    private void initListView(){

    }

    @Override
    public void onHiddenChanged(boolean hidden) { //监听隐藏变化
        super.onHiddenChanged(hidden);
        Log.d(TAG,"hidden-->"+2) ;
        //当true的时候隐藏，false的时候显示
        //当显示的时候加载用户的信息
        if( !hidden ){
            Log.d(TAG,"mAccessToken.getUid()-->"+mAccessToken.getUid());
            long uid = Long.valueOf(mAccessToken.getUid()).longValue();
            Log.d(TAG,"mAccessToken.getUid()-->"+uid);
            mUsersAPI.show(uid, new RequestListener() {
                @Override
                public void onComplete(String response) {
                    Log.d(TAG,"hidden-->response:"+response) ;
                    //数据获取成功后，将其保存在内存中或者其他地方，这里将其保存在application中
                    //application相当于单例
                    BaseApplication application = (BaseApplication) activity.getApplication() ;
                    Gson gson = new Gson() ;
                    application.currentUser = user = gson.fromJson(response,User.class) ;

                }

                @Override
                public void onWeiboException(WeiboException e) {
                    Log.d(TAG,"hidden-->response:"+e.getMessage()) ;

                }
            });
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_at :
                ToastUtils.showToast(activity,"@我的微博", Toast.LENGTH_LONG);
                intent2Activity(SelfStatuseActivity.class , "isAtWeibo");
                break ;
            case R.id.ll_comments :
                ToastUtils.showToast(activity,"@我的评论", Toast.LENGTH_LONG);
                intent2Activity(CommentToMeActivity.class );
                break ;
            case R.id.ll_like :

                break ;
            default:
                break;
        }
    }
}
